﻿using AccompanyCabinet.Helper;
using AccompanyCabinet.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace AccompanyCabinet.Admin
{
    public partial class ClearBox : Form
    {
        DataTable LocalDt = new DataTable();
        public ClearBox()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 每页记录数(默认：10)
        /// </summary>
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"].ToString());
        public int ppsize = 0;
        /// <summary>
        /// 总记录数(默认：0)
        /// </summary>
        public int recordCount = 0;
        /// <summary>
        /// 总页数(默认：0)
        /// </summary>
        public int pageCount = 0;
        /// <summary>
        /// 当前页(默认：1)
        /// </summary>
        public int currentPage = 1;
        private void ClearBox_Load(object sender, EventArgs e)
        {
            LocalDt.Columns.Add("dpLockId", typeof(int));
            LocalDt.Columns.Add("dpLockNum", typeof(int));
            Query_module();
        }
        public void Query_module()
        {
            dgv_boxclear.Columns.Clear();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("箱号", typeof(string)));
            dt.Columns.Add(new DataColumn("类型", typeof(string)));
            dt.Columns.Add(new DataColumn("状态", typeof(string)));
            DataRow drs = null;
            drs = dt.NewRow();
            storagePage inComes = new storagePage();
            string url = "TerminalInfo/DeviceBoxDetail";
            if (currentPage == 1) ppsize = pageSize;
            if (inComes.PageCount == 0) pageSize = ppsize;
            var res = HttpHelper.Get(url + "?dId=" + HttpHelper.deviceInfoModel.DId + "&pageIndex=" + currentPage + "&pageSize=" + pageSize + "");
            if (res.status == "OK")
            {
                inComes = JsonConvert.DeserializeObject<storagePage>(res.Message);
            }
            else
            {
                MessageBox.Show(res.Message);
                return;
            }
            pageCount = inComes.PageCount;
            currentPage = inComes.PageIndex;
            pageSize = inComes.PageSize;
            if (inComes.PageIndex == 1)
            {
                ppsize = pageSize;
            }
            if (pageCount == currentPage)
            {
                ppsize = pageSize;
                pageSize = inComes.TotalRows % pageSize == 0 ? pageSize : inComes.TotalRows % pageSize;
            }
            lab_totalnum.Text = inComes.TotalRows.ToString();
            lab_totalpage.Text = inComes.PageCount.ToString();
            label_pagesize.Text = inComes.PageSize.ToString();
            txt_currentpage.Text = inComes.PageIndex.ToString() + "/" + inComes.PageCount.ToString();
            for (int i = 0; i < pageSize; i++)
            {
                drs = dt.NewRow();
                drs[0] = inComes.LsList[i].Num;
                drs[1] = BoxType(inComes.LsList[i].TypeNo);
                drs[2] = StaNo(inComes.LsList[i].StatusNo);
                dt.Rows.Add(drs);
            }
            dgv_boxclear.DataSource = dt;
            dgv_boxclear.AllowUserToAddRows = false;
        }
        public string StaNo(string status)
        {
            switch (status)
            {
                case "Use": return "在用";
                case "Unuse": return "空闲";
                default: return "占用";
            }
        }
        public string BoxType(string status)
        {
            switch (status)
            {
                case "Big": return "大";
                case "Mid": return "中";
                case "Small": return "小";
                default: return "";
            }
        }

        private void btn_return_Click(object sender, EventArgs e)
        {
            this.Hide();
            new Index().Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgv_boxclear.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择箱子");
                return;
            }
            else
            {
                if (dgv_boxclear.SelectedRows.Count > 1)
                {
                    MessageBox.Show("只能选择一行进行打开");
                    return;
                }
                else
                {
                    int rowIndex;
                    try
                    {
                        rowIndex = dgv_boxclear.CurrentCell.RowIndex;
                    }
                    catch
                    {
                        rowIndex = 0;
                    }
                    int num = Convert.ToInt32(dgv_boxclear.Rows[rowIndex].Cells[0].Value);
                    XmlDocument xd = new XmlDocument();
                    xd.Load("../DeviceData.xml");//加载XML文件
                    XmlNodeList xmlNoteList = xd.GetElementsByTagName("box");
                    foreach (XmlElement item in xmlNoteList)
                    {
                        XmlNodeList xn = item.ChildNodes;
                        if (xn[1].InnerText==num.ToString())
                        {
                            DataRow dr = LocalDt.NewRow();
                            dr["dpLockId"] = xn[2].InnerText;
                            dr["dpLockNum"] = xn[3].InnerText;
                            LocalDt.Rows.Add(dr);
                        }
                    }
                    string url = "TerminalInfo/ResetBox";
                    var res = HttpHelper.Get(url + "?dId=" + HttpHelper.deviceInfoModel.DId + "&num=" + num + "");
                    if (res.status == "OK")
                    {
                        new OpenLock().OpenLockAction(LocalDt,1);
                        MessageBox.Show("开锁成功");
                    }
                    else { MessageBox.Show(res.Message);return; }
                    Query_module();
                    this.Refresh();
                    //foreach (var item in HttpHelper.storageList)
                    //{
                    //    if (item.Num == num) {
                    //        item.StatusNo = "Unuse";
                    //        string url = "/ResetBox";
                    //        var res = HttpHelper.Get(url+"?dId="+HttpHelper.deviceInfoModel.DId+"&num="+num+"");
                    //        if (res.status == "OK") MessageBox.Show("重置成功");
                    //        this.Hide();
                    //        new ClearBox().Show();
                    //        return;
                    //    } 
                    //}

                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //foreach (var item in HttpHelper.storageList)
            //{
            //    item.StatusNo = "Unuse";
            //}
            XmlDocument xd = new XmlDocument();
            xd.Load("../DeviceData.xml");//加载XML文件
            XmlNodeList xmlNoteList = xd.GetElementsByTagName("box");
            foreach (XmlElement item in xmlNoteList)
            {
                XmlNodeList xn = item.ChildNodes;
                DataRow dr = LocalDt.NewRow();
                dr["dpLockId"] = xn[2].InnerText;
                dr["dpLockNum"] = xn[3].InnerText;
                LocalDt.Rows.Add(dr);
            }
            string url = "TerminalInfo/ResetBox";
            var res = HttpHelper.Get(url+"?dId="+HttpHelper.deviceInfoModel.DId+"");
            if (res.status == "OK")
            {
                new OpenLock().OpenLockAction(LocalDt,1);
                MessageBox.Show("开锁成功");
            }
            else { MessageBox.Show(res.Message);return; }
            Query_module();
        }

        private void btn_Homepage_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            pageSize = ppsize;
            Query_module();
        }

        private void btn_prepage_Click(object sender, EventArgs e)
        {
            currentPage = currentPage - 1 > 0 ? currentPage - 1 : 1;
            if (currentPage + 1 >= pageCount) pageSize = ppsize;
            Query_module();
        }

        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            currentPage = currentPage + 1 > pageCount ? pageCount : currentPage + 1;
            if (currentPage + 1 > pageCount) pageSize = ppsize;
            Query_module();
        }

        private void btn_backpage_Click(object sender, EventArgs e)
        {
            currentPage = pageCount;
            if (currentPage + 1 > pageCount) pageSize = ppsize;
            Query_module();
        }
    }
}
